package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xin.di.uav.applet.mapper.ServiceAreaMapper;
import com.xin.di.uav.applet.request.MerchantAddParam;
import com.xin.di.uav.applet.request.MerchantEditParam;
import com.xin.di.uav.applet.resp.MerchantResp;
import com.xin.di.uav.applet.service.MerchantService;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.response.IdAndNameResp;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.operate.mapper.MerchantMapper;
import com.xin.di.uav.po.GoodsMapper;
import com.xin.di.uav.user.order.po.MerchantPO;
import com.xin.di.uav.user.order.po.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ServiceAreaMapper serviceAreaMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public boolean add(MerchantAddParam merchantAddParam) {
        MerchantPO merchantPO = BeanUtil.copyProperties(merchantAddParam, MerchantPO.class);
        merchantPO.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        return merchantMapper.insert(merchantPO) > 0;
    }

    @Override
    public boolean delete(String id) {
        return merchantMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(MerchantEditParam merchantEditParam) {
        String id = merchantEditParam.getId();
        MerchantPO merchantPO = merchantMapper.selectById(id);
        if (merchantPO == null) {
            throw new BusinessException("记录不存在");
        }
        merchantPO = BeanUtil.copyProperties(merchantEditParam, MerchantPO.class);
        return merchantMapper.updateById(merchantPO) > 0;
    }

    @Override
    public boolean updateStatus(IdRequest idRequest) {
        String id = idRequest.getId();
        MerchantPO serviceAreaPO = merchantMapper.selectById(id);
        if (serviceAreaPO == null) {
            throw new BusinessException("记录不存在");
        }
        boolean status = serviceAreaPO.getStatus();
        serviceAreaPO.setStatus(!status);
        return merchantMapper.updateById(serviceAreaPO) > 0;
    }

    @Override
    public MerchantPO getById(String id) {
        return merchantMapper.selectById(id);
    }

    @Override
    public MerchantResp list(int pageNum, int pageSize, String merchantName) {
        pageNum = (pageNum - 1) * pageSize;
        MerchantResp merchantResp = new MerchantResp();
        int cnt = merchantMapper.count(merchantName);
        if (cnt > 0) {
            merchantResp.setTotal(cnt);
            List<MerchantPO> merchantPOS = merchantMapper.selectList(pageNum, pageSize, merchantName);
            List<MerchantResp.Record> recordList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(merchantPOS)) {
                List<String> scenicSpotIds = merchantPOS.stream().map(MerchantPO::getScenicSpotId).toList();
                List<String> merchantIds = merchantPOS.stream().map(MerchantPO::getId).toList();
                List<Product> products = goodsMapper.selectByMerchantIds(merchantIds);
                List<IdAndNameResp> idAndNameRespList = serviceAreaMapper.selectIdAndName(scenicSpotIds);
                for (MerchantPO merchantPO : merchantPOS) {
                    List<Product> products1 = products.stream().filter(product -> product.getMerchantId().equals(merchantPO.getId())).toList();

                    MerchantResp.Record record = BeanUtil.copyProperties(merchantPO, MerchantResp.Record.class);
                    for (IdAndNameResp idAndNameResp : idAndNameRespList) {
                        if (idAndNameResp.getId().equals(merchantPO.getScenicSpotId())) {
                            record.setServiceAreasId(idAndNameResp.getId());
                            record.setServiceAreas(idAndNameResp.getName());
                        }
                    }
                    if (CollectionUtil.isNotEmpty(products1)) {
                        List<MerchantResp.ProductRecord> productRecords = BeanUtil.copyProperties(products1, MerchantResp.ProductRecord.class);
                        record.setProductRecords(productRecords);
                    }
                    recordList.add(record);
                }
            }

            merchantResp.setRecordList(recordList);
        }


        return merchantResp;
    }

    @Override
    public List<IdAndNameResp> getIdAndName(String serviceRegion) {
        return merchantMapper.searchList(serviceRegion);
    }
}
